/**
 * 
 */
package com.radeonsys.litmus.data.schema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.Assert;

import com.radeonsys.litmus.data.FieldNotFoundException;
import com.radeonsys.litmus.data.converters.Converter;

/**
 * @author Ranganath Kini (ranganathkini at gmail dot com)
 * @version 1.0
 *
 */
class DefaultSchema implements Schema {
	
	private final Map<String, Converter<?, ?>> _fieldMap;

	/**
	 * Creates a new instance with the specified list of field entries
	 */
	DefaultSchema(final Map<String, Converter<?, ?>> fieldMap) {
		Assert.notNull(fieldMap, "A valid field map must be specified");
		Assert.notEmpty(fieldMap, "Specified field map cannot be empty");
		
		_fieldMap = new LinkedHashMap<String, Converter<?,?>>(fieldMap);
	}

	/* (non-Javadoc)
	 * @see com.radeonsys.litmus.data.schema.Schema#getFieldCount()
	 */
	public int getFieldCount() {
		return _fieldMap.size();
	}

	/* (non-Javadoc)
	 * @see com.radeonsys.litmus.data.schema.Schema#getFieldMap()
	 */
	public Map<String, Converter<?,?>> getFieldMap() {
		return Collections.unmodifiableMap(_fieldMap);
	}

	/* (non-Javadoc)
	 * @see com.radeonsys.litmus.data.schema.Schema#getFieldNames()
	 */
	public List<String> getFieldNames() {
		return new ArrayList<String>(getFieldMap().keySet());
	}

	/* (non-Javadoc)
	 * @see com.radeonsys.litmus.data.schema.Schema#getFieldType(java.lang.String)
	 */
	public Converter<?,?> getFieldConverter(final String fieldName) {
		Assert.notNull(fieldName, "Field name must be specified");
		if (!hasField(fieldName))
			throw new FieldNotFoundException(fieldName);
		return _fieldMap.get(fieldName);
	}

	/* (non-Javadoc)
	 * @see com.radeonsys.litmus.data.schema.Schema#hasField(java.lang.String)
	 */
	public boolean hasField(final String fieldName) {
		Assert.notNull(fieldName, "Field name must be specified");
		
		return _fieldMap.containsKey(fieldName);
	}

}
